import java.io.IOException;
import java.util.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sessionbeans.RegistrationBeanRemote;


/**
 * Servlet implementation class RegistrationServlet
 */
public class RegistrationServlet extends HttpServlet {
	private static final String REGISTRATION_PERFORMED = "Registration performed, redirecting to login page...";
	private static final String MISSING_OR_WRONG_DATA = "Missing or wrong data...";
	private static final long serialVersionUID = 1L;
	private String[] parameterNames = {"username","password","name","surname","e-mail","address","town","ability","confirm"};
    /**
     * Default constructor. 
     */
    public RegistrationServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<String> parameters = new ArrayList<String>();
		
		for (String s: parameterNames) {
			if (request.getParameter(s) != null) {
				if (!(s.equals("ability")))
					parameters.add(request.getParameter(s));
			}
		}
		
		if (request.getParameterValues("ability") != null) {
			parameters.add(request.getParameterValues("ability")[0]);
		}
		
		if (parameters.size() == parameterNames.length) {
			InitialContext context;
			try {
				context = new InitialContext();
				Object ref = context.lookup("RegistrationBeanRemote");
	    		RegistrationBeanRemote registrationBean = (RegistrationBeanRemote)PortableRemoteObject.narrow(ref, RegistrationBeanRemote.class);
	    		if(parameters.get(1).equals(parameters.get(7))){
	    			if (registrationBean.createNewUser(parameters.get(0),parameters.get(1),parameters.get(2),parameters.get(3),parameters.get(4),parameters.get(5),parameters.get(6),parameters.get(8))) {
						request.getSession().setAttribute("redirected","Index.jsp");
						request.getSession().setAttribute("message",REGISTRATION_PERFORMED);
						response.sendRedirect("Message.jsp");
					}
					else {
						request.getSession().setAttribute("redirected","Registration.jsp");
						request.getSession().setAttribute("message",MISSING_OR_WRONG_DATA);
						response.sendRedirect("Message.jsp");
					}
	    		} else {
	    			request.getSession().setAttribute("redirected","Registration.jsp");
					request.getSession().setAttribute("message",MISSING_OR_WRONG_DATA);
					response.sendRedirect("Message.jsp");
	    		}
				
			} catch (NamingException e) {
				
			}
		}
		else {
			request.getSession().setAttribute("redirected","Registration.jsp");
			request.getSession().setAttribute("message",MISSING_OR_WRONG_DATA);
			response.sendRedirect("Message.jsp");
		}
	}

}
